﻿#include "QuestDescEx.h"

#include <DescStore/QuestDesc.h>


QuestDescEx::QuestDescEx()
{
	if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetCreateMode())
	{
		CreateInit();
	}
	else
	{
		ResumeInit();
	}
}

QuestDescEx::~QuestDescEx()
{
}

int QuestDescEx::CreateInit()
{
	return 0;
}

int QuestDescEx::ResumeInit()
{
	return 0;
}

int QuestDescEx::Load()
{
	return 0;
}

int QuestDescEx::CheckWhenAllDataLoaded()
{
	m_event_map.clear();
	m_event_map.resize(m_event_map.max_size());
	for (int i = 0; i < (int)QuestDesc::Instance()->GetResNum(); i++)
	{
		auto pDesc = QuestDesc::Instance()->GetDescByIndex(i);
		if (pDesc == NULL) continue;

		for (int r = 0; i < (int)pDesc->complete_random_item.size(); r++)
		{
			pDesc->complete_random_item_weight_sum += pDesc->complete_random_item[i].weight;
		}

		bool complete_item = false;
		size_t condition_num = 0;
		for (int j = 0; j < (int)pDesc->complete_item.size(); ++j)
		{
			if (pDesc->complete_item[j].id > 0 && pDesc->complete_item[j].num > 0)
			{
				complete_item = true;
				break;
			}
		}

		if (pDesc->complete_random_item_weight_sum > 0 || complete_item)
		{
			m_event_map[E_QUEST_EVENT_CONSUME_ITEM].SetBit(pDesc->id);
			++condition_num;
			if (pDesc->complete_use_item_type > 0)
			{
				LOGSVR_ERROR("can not use item and consume item: " << pDesc->id);
				return -1;
			}
		}
		if (pDesc->complete_level > 0)
		{
			m_event_map[proto_ff::E_QUEST_EVENT_LEVEL_UP].SetBit(pDesc->id);
			++condition_num;
		}
		for (int k = 0; k < (int)pDesc->complete_kill_monster_list.size(); ++k)
		{
			if (pDesc->complete_kill_monster_list[k].id > 0 && pDesc->complete_kill_monster_list[k].num > 0)
			{
				m_event_map[proto_ff::E_QUEST_EVENT_KILL_MONSTER].SetBit(pDesc->id);
				++condition_num;
				break;
			}
		}
		if (pDesc->complete_level_id > 0)
		{
			if (pDesc->complete_level_star)
			{
				m_event_map[proto_ff::E_QUEST_EVENT_COMPLETE_LEVEL_STAR].SetBit(pDesc->id);
			}
			else
			{
				m_event_map[proto_ff::E_QUEST_EVENT_COMPLETE_LEVEL].SetBit(pDesc->id);
			}
			++condition_num;
		}
		if (pDesc->complete_join_guild)
		{
			m_event_map[proto_ff::E_QUEST_EVENT_JOIN_GUILD].SetBit(pDesc->id);
			++condition_num;
		}
		if (pDesc->complete_equip_strength_times > 0)
		{
			m_event_map[proto_ff::E_QUEST_EVENT_STRENGTHEN].SetBit(pDesc->id);
			++condition_num;
		}
		if (pDesc->complete_level_mode_times > 0)
		{
			m_event_map[proto_ff::E_QUEST_EVENT_COMPLETE_LEVEL].SetBit(pDesc->id);
			++condition_num;
		}
		if (pDesc->complete_levels_id_list.size() > 0)
		{
			if (pDesc->complete_level_star)
			{
				m_event_map[proto_ff::E_QUEST_EVENT_COMPLETE_LEVEL_STAR].SetBit(pDesc->id);
				++condition_num;
			}
			else if (pDesc->complete_level_time)
			{
				m_event_map[proto_ff::E_QUEST_EVENT_COMPLETE_LEVEL].SetBit(pDesc->id);
				++condition_num;
			}
			else if (pDesc->complete_enter_any_level)
			{
				if (pDesc->complete_enter_any_level && pDesc->complete_levels_win)
				{
					m_event_map[proto_ff::E_QUEST_EVENT_COMPLETE_LEVEL].SetBit(pDesc->id);
					++condition_num;
				}
				else
				{
					m_event_map[proto_ff::E_QUEST_EVENT_ENTER_LEVEL].SetBit(pDesc->id);
					++condition_num;
				}
			}
		}
		if (pDesc->complete_unlock_skill_order > 0)
		{
			m_event_map[proto_ff::E_QUEST_EVENT_UNLOCK_SKILL_ORDER].SetBit(pDesc->id);
			++condition_num;
		}
		if (pDesc->complete_skill_awake)
		{
			m_event_map[proto_ff::E_QUEST_EVENT_COMPLETE_SKILL_AWAKE].SetBit(pDesc->id);
			++condition_num;
		}
		if (pDesc->complete_instance)
		{
			m_event_map[proto_ff::E_QUEST_EVENT_COMPLETE_INSTANCE].SetBit(pDesc->id);
			++condition_num;
		}
		if (pDesc->complete_about_loop_id && pDesc->complete_about_loop_quest_times)
		{
			m_event_map[proto_ff::E_QUEST_EVENT_COMPLETE_LOOP_QUEST_WITH_TIMES].SetBit(pDesc->id);
			++condition_num;
		}
		if (pDesc->complete_skill_transfer > 0)
		{
			m_event_map[proto_ff::E_QUEST_EVENT_COMPLETE_SKILL_TRANSFER].SetBit(pDesc->id);
			++condition_num;
		}

		for (int k = 0; k < (int)pDesc->complete_find_npc.size(); ++k)
		{
			if (pDesc->complete_find_npc[k] > 0)
			{
				m_event_map[proto_ff::E_QUEST_EVENT_DAILOG_WITH_NPC].SetBit(pDesc->id);
				++condition_num;
			}
		}

		// award:
		if (pDesc->award_team_master_drop || pDesc->award_team_master_exp)
		{
			pDesc->award_check_team_leader = true;
		}
		if (pDesc->award_check_team_leader || pDesc->award_team_member_by_count)
		{
			pDesc->award_check_team = true;
		}

		while (pDesc->quest_prev.size() > 0 && pDesc->quest_prev.front() == 0)
		{
			pDesc->quest_prev.erase(pDesc->quest_prev.begin());
		}

		while (pDesc->quest_prev.size() > 0 && pDesc->quest_prev.back() == 0)
		{
			pDesc->quest_prev.pop_back();
		}

		QuestNode node;;
		CHECK_EXPR(condition_num <= node.event_node_list.max_size(), -1, "Conditon Too more:{} QuestID:{}", condition_num, pDesc->id);
	}
	return 0;
}

bool QuestDescEx::CheckEvent(QuestEvent ev, int iQuestID) const
{
	CHECK_EXPR(iQuestID > 0 && iQuestID < MAX_QUEST_ID_VALUE, false, "id error : {}", iQuestID);
	return m_event_map[ev].IsBitSetted(iQuestID);
}
